/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.sesame.domain.Album;
import com.sesame.domain.Guestbook;
import com.sesame.domain.Image;
import com.sesame.domain.User;
import com.sesame.service.AlbumService;
import com.sesame.service.ContactService;
import com.sesame.service.UserService;
import com.sesame.toolkit.Page;

/**
 * @author voidman
 * 
 */
public class ShowAllGuestbookController extends AbstractController {

    private ContactService contactService;
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setContactService(ContactService contactService) {
        this.contactService = contactService;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        Integer userId = (Integer) request.getSession().getAttribute("userId");
        String nickName = (String) request.getSession()
                .getAttribute("nickName");
        String headPic = (String) request.getSession().getAttribute("headPic");
        String content = request.getParameter("content");
        String reply = request.getParameter("reply");
        Integer guestbookId = null;
        Integer user2Id = null;
        Map model = new HashMap();
        
        if (request.getParameter("userId") == null) {
            return new ModelAndView("redirect:home.html");
        }
        
        if (request.getParameter("guestbookId") != null) {
            guestbookId = Integer.parseInt(request.getParameter("guestbookId"));
        }

        if (request.getParameter("userId") != null) {
            user2Id = Integer.parseInt(request.getParameter("userId"));
            User user = userService.showUser(user2Id);
            model.put("user", user);

        }
        if (content != null && !content.equals("") && userId != null) {
            contactService.newGuestbook(userId, user2Id, content, nickName,
                    headPic);
        }

        if (reply != null && guestbookId != null && !reply.equals("")) {
            contactService.reply(reply, guestbookId);
        }

        List<Guestbook> guestbookList = null;

        guestbookList = contactService.listAllGuestbook(user2Id);

        model.put("guestbookList", guestbookList);
        model.put("hostId", user2Id);

        return new ModelAndView("showAllGuestbook", model);
    }

}